import router from '../router'
import store from './index'

// 跳转到登录页
const toLogin = () => {
    ElMessageBox.confirm(
      '你还没有登录，立即去登录？',
      '提示',
      {
        confirmButtonText: '登录',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    .then(() => {
        router.push('/login')
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消登录',
      })
    })
    
  }

  
router.beforeEach((to,from,next) => {
    
    console.log(to.path);
    // 判断是否需要鉴权
    if (to.meta.isAuth){
      if (store.state.loginFlag) {
        next()
      }else {
        toLogin()
      }
    }else{
      next()
    }
  })

// 页面跳转时更改页面标题
router.afterEach((to,from) => {
    document.title = to.meta.title
})